package cloudgo

import (
	"html/template"
	"math/rand"
	"net/http"
	"strconv"
	"github.com/unrolled/render"
)

func LuckynumMaker(formatter *render.Render) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		luckynum := rand.Intn(2333)
		formatter.JSON(w, http.StatusOK, struct {
			Luckynum int
		}{Luckynum: luckynum})
	}
}

func homeHandler(formatter *render.Render) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		template := template.Must(template.New("index.html").ParseFiles("./html/index.html"))
		_ = template.Execute(w, struct {
			ID      string
			Content string
		}{ID: "cool student", Content: "Welcome to Sun Yat-sen University!"})
	}
}

func checkform(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	username := template.HTMLEscapeString(r.Form.Get("username"))
	password := template.HTMLEscapeString(r.Form.Get("password"))
	luckynum := strconv.Itoa(rand.Intn(2333))
	t := template.Must(template.New("login.html").ParseFiles("./html/login.html"))
	err := t.Execute(w, struct {
		Username string
		Password string
		Luckynum  string
	}{Username: username, Password: password, Luckynum: luckynum})
	if err != nil {
		panic(err)
	}
}

